package com.example.apps

import android.util.Log
import androidx.lifecycle.viewModelScope
import com.example.apps.entity.AppMuiltEntity
import com.example.lib_base.BaseViewModel
import com.example.lib_http.download.DownState
import com.example.lib_http.download.DownloadManager
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.onCompletion
import kotlinx.coroutines.flow.onStart
import kotlinx.coroutines.launch
import java.io.File

class NetViewModel:BaseViewModel<NetRepository,NetAppState>() {
    override fun createState(): NetAppState {
        return NetAppState(false,false,false,false, mutableListOf())
    }

    override fun createResp(): NetRepository {
        return NetRepository()
    }

    fun selClientApp(){
        var map=HashMap<String,Any>()
        map.put("userId",1)
        map.put("page",1)

        viewModelScope.launch {
            resp.selClientApp(map)
                .flowOn(Dispatchers.IO)
                .catch {
                    it.printStackTrace()
                }
                .onStart {
                    mutableContainer.updateRequestState { copy(request = true,complete = false) }
                }
                .onCompletion {
                    mutableContainer.updateRequestState { copy(request = false,complete = true) }
                }.collect{
                    Log.i("===",it.toString())
                    val list = ArrayList<AppMuiltEntity>()
                    it.data.forEach {
                        val entity = AppMuiltEntity("","",0,"","","",0.0,
                            0,0,"","","","","",0,0,"",
                            "",it.title,1)
                        list.add(entity)
                        val alist = it.appsList
                        for (i in 0..alist.size-1){
                            var en= alist[i]
                            val entity2 = AppMuiltEntity(en.apkUrl,en.appIcon,en.appId,en.appImgs,en.appIntroduction,en.appName,en.appSize,
                                en.appType,en.appUserId,en.app_package,en.netUrl,en.operationFlag,en.trustFlag,en.updateStr,en.updateTime,
                                en.uploadTime,en.userName,en.versionCode,"",2)
                            list.add(entity2)
                        }
                    }
                    mutableContainer.updateUiState {
                        copy(true,false,false,false, list)
                    }
                }
        }
    }

    fun download(apkUrl: String, path: String) {
        viewModelScope.launch {
            DownloadManager.download(apkUrl,File(path))
                .catch {
                    it.printStackTrace()
                }
                .onStart {
                    mutableContainer.updateUiState { copy(false,true,false,false, mutableListOf()) }
                }.collect{
                    if(it is DownState.Done){
                        mutableContainer.updateUiState { copy(false,false,false,true, mutableListOf()
                        )
                        }
                    }
                    else if(it is DownState.Error){

                    }else{

                    }
                }
        }
    }
}